#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : 全球法律在线项目                #
#                                                                   #
#                   @File Name    : mys_ejugov_jud.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2020-10-23 17:21                 #
#                                                                   #
#                   @Last Update  : 2020-10-23 17:21                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import hashlib

import scrapy
from ..items import MyFileItem
import re


class Spiders (scrapy.Spider):
    name = 'mys_ejugov_jud'
    allowed_domains = ['http://ejudgment.kehakiman.gov.my/portal/']
    # 马来西亚判例数据地址
    start_urls = ['http://ejudgment.kehakiman.gov.my/portal/ap_list_alle.php?']
    id = 0

    # 解析初始页面
    def parse(self, response):
        # 获得成员列表
        trs = response.xpath('//*[@id="CUSTOM_Ap"]/tbody/tr')
        for tr in trs:
            # 裁决日期
            AdjudicationDateS = tr.xpath('./td[2]/text()')
            AdjudicationDate = ''
            if len(AdjudicationDateS):
                for AdjudicationDateSl in AdjudicationDateS:
                    AdjudicationDate = AdjudicationDate + ' ' + AdjudicationDateSl.get().strip()
                AdjudicationDate = re.sub(r'''^ *''', r'''''', AdjudicationDate)  # 去除开头的空格
            # 判例编号
            CaseNumberS = tr.xpath('./td[3]/text()')
            CaseNumber = ''
            if len(CaseNumberS):
                for CaseNumberSl in CaseNumberS:
                    CaseNumber = CaseNumber + ' ' + CaseNumberSl.get().strip()
                CaseNumber = re.sub(r'''^ *''', r'''''', CaseNumber)  # 去除开头的空格
            # 当事人
            # PeopleS = tr.xpath('./td[4]//text()')
            # People = ''
            # if len(PeopleS):
            #     for PeopleSl in PeopleS:
            #         People = People + ' ' + PeopleSl.get().strip()
            #     People = re.sub(r'''^ *''', r'''''', People)  # 去除开头的空格
            #     People = re.sub(r'''\n*\r*\t*''', r''' ''', People)   # 去除空白字符 [ \f\n\r\t\v]
            # 全文网址
            detailUrlS = tr.xpath('./td[6]/a/@onclick')
            if len(detailUrlS):
                detailUrlstr = detailUrlS.get().strip()
                detailUrll = re.findall(r'''(?<=href=').*?(?=')''', detailUrlstr)
                detailUrl = 'http:' + detailUrll[0]
                yield response.follow(detailUrl, callback=self.detailparse, dont_filter=True,
                                      meta={'AdjudicationDate': AdjudicationDate, 'CaseNumber': CaseNumber})
        # 切换到下一页
        nextS = response.xpath('//input[@name="btnNext"]/@onclick')
        if len(nextS):
            nextstr = nextS.get().strip()
            nextUrll = re.findall(r'''(?<=href=').*?(?=\';)''', nextstr)
            nextUrl = self.allowed_domains[0] + nextUrll[0]
            yield response.follow(nextUrl, callback=self.parse, dont_filter=True)

    # 解析年份成员列表页
    def detailparse(self, response):
        detailUrls = response.xpath('//*[@id="div_ksprint"]/table/tbody[2]/tr[2]/td/a')
        metaa = response.meta
        if len(detailUrls):
            detailUrla = response.xpath('//*[@id="div_ksprint"]/table/tbody[2]/tr[2]/td/a')[0]
            yield response.follow(detailUrla, callback=self.detailed, dont_filter=True, meta=metaa)

    # 解析详情页
    def detailed(self, response):
        item = MyFileItem()
        if response.status == 200:

            # 法律详细网址
            detailUrl = response.url
            # 案例编号
            CaseNumber = response.meta['CaseNumber']
            # 裁决日期
            AdjudicationDate = response.meta['AdjudicationDate']
            # 当事人
            # People = response.meta['People']

            country = 'Malaysia'
            website = 'ejudgment'
            modular = 'Judiciary'
            # 下载文件格式
            ext = 'pdf'

            # 下载文件名
            fina = ''
            if len(detailUrl) > 0:
                fina = 'f' + str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())

            # 唯一ID
            systemid = str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())

            item['file_urls'] = ''
            item['country'] = country
            item['website'] = website
            item['modular'] = modular
            item['ext'] = ext
            item['fina'] = fina
            item['title'] = ''
            item['abstractUrl'] = ''
            item['abstract'] = ''
            item['dabstractUrl'] = ''
            item['detail'] = ''
            item['detailUrl'] = ''
            item['downloadUrl'] = detailUrl

            item['Title'] = ''
            item['CaseNumber'] = CaseNumber
            item['KeyWord'] = ''
            item['SortA'] = 'LAWCOUNTRYMLXY'
            item['People'] = ''
            item['CaseOfAction'] = ''
            item['UseLaw'] = ''
            item['AdjudicationDate'] = AdjudicationDate
            item['FullText'] = ''
            item['JudgAgency'] = ''
            item['SortB'] = 'LANGUAGEMLY'
            item['SortC'] = ''
            item['CaseSummary'] = ''
            item['Articles'] = ''
            item['Chapter'] = ''
            item['Section'] = ''
            item['SYS_FLD_DIGITFILENAME'] = fina
            item['FileUrl'] = ''
            item['AbstractFileName'] = ''
            item['DownLoadUrl'] = detailUrl
            item['DownLoadWebNameC'] = '马来西亚联邦法院首席司法常务官办公室'
            item['DownLoadWebNameE'] = "Pejabat ketua pemdaftar mahkamah persekutuam malaysla"
            item['SYSID'] = systemid
            item['Website'] = 'Judiciary'
            item['Isconversion'] = '0'
            item['CaseDate'] = ''

            yield item

